﻿#include "GameCore.h"

GameCore::_GameStatus GameCore::HuiHeKaiShiChuLi(int player)
{
    return HuiHeKaiShiJiNeng(player);
}

GameCore::_GameStatus GameCore::PanDingChuLi(int player)
{
    bool bShanDianIsMine = false; //是否闪电判定后又装在了自己身上
    while(m_arrayPlayer[player].m_listPanDing.count() > 0)    //开始 判定
    {
        /*
        switch(Player[player].pandingpai[i].leixing)
        {
        case 251:
            break;
        case 252:
            break;
        default:
            break;
        }
        //pai p=Player[player].pandingpai[0];
        for(;;)//在判定之前询问【无懈】
        {
        i=Wuxie(&p,player2,player1,qipaidui,qipaishu);
        if(!i)break;
        j++;
        i=Wuxie(&p,player1,player2,qipaidui,qipaishu);
        if(!i)break;
        j++;
        }
        */
        if(m_arrayPlayer[player].m_listPanDing.count() == 1 && bShanDianIsMine == true)
            break;  //已经判定过闪电

        _Pai* pPanDing = m_arrayPlayer[player].getPandingData(0);
        switch(pPanDing->value())    //判定类型
        {
        case _Pai::Value_LeBuSiShu:        //乐
            {
                //    if(j%2 == 0)
                /*
                {
                printf("%s的判定牌是：",PAI_251);
                if((Panding(paidui,paiduishu,qipaidui,qipaishu).huase)!=1)
                {
                printf("%s判定成功！",PAI_251);
                Player[player].NameString();
                printf("跳过出牌阶段！");
                K=-1;
                }
                else printf("%s判定失败！",PAI_251);
                }
                */
                m_strMsg = m_hashGameMessage[_Pai::Value_LeBuSiShu][2]
                        .arg(_Pai::getName(_Pai::Value_LeBuSiShu));
                emit s_Message(m_strMsg);
                //ShowChuPaiMessage(m_strMsg, 0);
                //Sleep(config.configHash["DELAY_TIME_COMPUTER"].toInt());

                {
                    //str = m_hashGameMessage[_Pai::Value_LeBuSiShu][2]
                    //        .arg(pPanDing->getName())
                    //        .arg(t_n);
                            //.arg(getPlayerShowName(player));

                    if(WenTi(player, m_strMsg, 0))
                    {
                        if(gameStatus() != Game_Status_Continue)return gameStatus();

                        //emit s_Message(_T("回答正确!"));
                        //ShowChuPaiMessage(str);
                        QiPai(m_arrayPlayer[player].qiPanDing(0));
                        emit s_RefreshAllPokePosition();
                    }
                    else
                    {
                        if(gameStatus() != Game_Status_Continue)return gameStatus();

                        QiPai(m_arrayPlayer[player].qiPanDing(0));
                        emit s_RefreshAllPokePosition();

                        m_arrayPlayer[player].m_nChuPaiJieDuan = -1;

                        m_strMsg = m_hashGameMessage[_Pai::Value_LeBuSiShu][3]
                                .arg(getPlayerShowName(player));
                        emit s_Message(m_strMsg);
                        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
                    }
                }
                /*
                int t_n = Random(1,7);
                if(t_n == 6)
                else
                {
                    str = _T("%1判定结果%2，判定失败")
                            .arg(pPanDing->getName())
                            .arg(t_n);
                            //.arg(getPlayerShowName(player));
                    m_arrayPlayer[player].chupaijieduan = 0;
                    emit s_Message(str);
                    ShowChuPaiMessage(str);
                }*/

                //Sleep(m_ComputerDelay);
            }
            break;

        case _Pai::Value_ShanDian:    //闪电
            {
                int &nJiNengTimeIndex = pPanDing->listJiNeng().at(0)->m_nJiNengParam;
                m_strMsg = _T("%1 发动中")
                        .arg(_Pai::getName(_Pai::Value_ShanDian));
                emit s_Message(m_strMsg);


                int time;
                if(m_arrayPlayer[player].isMe())    //如果是自己,则按递减时间来
                    time = _JiNeng::arrayTuoFuTime[nJiNengTimeIndex];
                else    //如果是其他人,则默认时间
                    time = 0;

                bool bRet = WenTi(player, m_strMsg, time);
                if(gameStatus() != Game_Status_Continue)return gameStatus();
                if(bRet)
                {
                    /*
                    m_strMsg = _T("")
                            .arg(_Pai::getName(_Pai::Value_ShanDian))
                            .arg(t_ShaiDian);
                    emit s_Message(m_strMsg);
                    ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);*/
                    //emit s_Message(_T("回答正确!"));
                    //ShowChuPaiMessage(m_strMsg);
                    if(nJiNengTimeIndex + 1 < (int)(sizeof(_JiNeng::arrayTuoFuTime) / sizeof(qint32)))
                        nJiNengTimeIndex ++;

                    _Pai *pShanDian = m_arrayPlayer[player].qiPanDing(0);
                    int pp = GetNextPlayer(player);
                    while(pp != player)    //找到没有闪电的
                    {
                        if(ShanDian(pp, pShanDian) == 0)    //已经有
                        {
                            pp = GetNextPlayer(pp);
                        }
                        else break;
                    }
                    if(pp == player)        //到自己本身
                    {
                        ShanDian(pp, pShanDian);
                        bShanDianIsMine = true;
                    }
                }
                else
                {
                    /*
                    m_strMsg = m_hashGameMessage[_Pai::Value_ShanDian][2]
                            .arg(_Pai::getName(_Pai::Value_ShanDian))
                            .arg(t_ShaiDian);
                            .arg(getPlayerShowName(player));
                    emit s_Message(m_strMsg);*/

                    QiPai(m_arrayPlayer[player].qiPanDing(0));
                    emit s_RefreshAllPokePosition();

                    nJiNengTimeIndex = 0;

                    if(DiaoXue(2,player,-1,_Pai::Value_ShanDian) != Game_Status_Continue)
                        return gameStatus();

                    if(m_arrayPlayer[player].dead() == true)
                        return gameStatus();
                }

                //Sleep(m_ComputerDelay);


                //晒点功能(暂时不用)
                /*
                //if(!(j%2))
                {
                    int t_ShaiDian;
                    t_ShaiDian = ShaiDian();
                    if(gameStatus() != Game_Status_Continue)return gameStatus();

                    if(t_ShaiDian == 6)    //晒点成功
                    {
                        m_strMsg = m_hashGameMessage[_Pai::Value_ShanDian][1]
                                .arg(_Pai::getName(_Pai::Value_ShanDian))
                                .arg(t_ShaiDian);
                                //.arg(getPlayerShowName(player));
                        emit s_Message(m_strMsg);
                        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

                        //QString tstr = m_strMsg;
                        int n = GetNextPlayer(player);
                        while(n != player)
                        {
                            if(WenTi(player, m_strMsg, 0))
                            {
                                if(gameStatus() != Game_Status_Continue)return gameStatus();
                                //emit s_Message(_T("回答正确!"));
                                //ShowChuPaiMessage(m_strMsg);
                            }
                            else
                            {
                                if(gameStatus() != Game_Status_Continue)return gameStatus();
                                //m_strMsg = m_hashGameMessage[_Pai::Value_ShanDian][2]
                                //        .arg(_Pai::getName(_Pai::Value_ShanDian))
                                //        .arg(t_ShaiDian);
                                        //.arg(getPlayerShowName(player));
                                //emit s_Message(m_strMsg);
                                //ShowChuPaiMessage(m_strMsg);

                                if(DiaoXue(1,player,-1,_Pai::Value_Null) != Game_Status_Continue)return gameStatus();

                                if(m_arrayPlayer[player].dead() == true)
                                    return gameStatus();
                            }

                            n = GetNextPlayer(n);
                        }

                        QiPai(m_arrayPlayer[player].qiPanDing(0));
                        //emit s_RefreshAllPokePosition();

                        //if(ret == -1)
                        //    if(SetPlayerDead(player)==1)
                        //        return -1;
                    }
                    else
                    {
                        m_strMsg = m_hashGameMessage[_Pai::Value_ShanDian][2]
                                .arg(_Pai::getName(_Pai::Value_ShanDian))
                                .arg(t_ShaiDian);
                        emit s_Message(m_strMsg);
                        ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);

                        _Pai *pShanDian = m_arrayPlayer[player].qiPanDing(0);
                        int pp = GetNextPlayer(player);
                        while( pp != player )    //找到没有闪电的
                        {
                            if(m_arrayPlayer[pp].getPanDingPai(pShanDian) == 0)    //已经有
                            {
                                pp = GetNextPlayer(pp);
                            }
                            else break;
                        }
                        if(pp == player)        //到自己本身
                        {
                            m_arrayPlayer[pp].getPanDingPai(pShanDian);
                            bShanDianIsMine = true;
                        }

                        m_strMsg = m_hashGameMessage[_Pai::Value_ShanDian][3]
                                .arg(getPlayerShowName(pp))
                                .arg(_Pai::getName(_Pai::Value_ShanDian));
                        emit s_Message(m_strMsg);
                    }
                    //Sleep(m_ComputerDelay);
                }*/
            }
            break;

        default:
            emit s_Critical(_T("[!GameCore]判定牌 ERROR:%1!").arg(pPanDing->value()));
            return setGameStatus(Game_Status_ERROR);
        }
    }
    return gameStatus();
}

GameCore::_GameStatus GameCore::MoPaiChuLi(int player)
{
    if(!MoPai(player,2))
    {
        emit s_Critical(_T("[!GameCore]WuZhong ERROR!"));
        return setGameStatus(Game_Status_ERROR);
    }
    emit s_RefreshAllPokePosition();
    //RefreshUI();
    return gameStatus();
}

GameCore::_GameStatus GameCore::ChuPaiChuLi(int player)
{
    GameCore::_GameButtonValue ret;
    //m_arrayPlayer[player].comIndex = 0;

    for(;;)
    {
        if(m_arrayPlayer[player].dead() == true)
            return gameStatus();

        /*
        str = _T("%1出牌阶段")
                .arg(getPlayerShowName(player));
        emit s_Message(str);
        ShowChuPaiMessage(str);
        */

        ret = ChuPai(player,GameCore::Game_ChuPai_Type_ZhuDong,0);

        //m_arrayPlayer[player].resetAllFlags();
        //RefreshUI();

        //调用相关牌处理函数
        if(ret == Game_Button_OK)
        {
            //setGameStatus(Game_Status_Continue);
            switch(m_arrayStep[0].nSelectType[0])
            {
            case _BaseObject::Area_ShouPai:
                {
                    switch(m_arrayPlayer[player].getShouPaiData(m_arrayStep[0].nSelectValue[0])->value())
                    {
                    case _Pai::Value_Sha:
                        XiaoGuoQiPai();
                        Sha(player,m_arrayStep[1].nSelectPlayerID);
                        break;
                    case _Pai::Value_Tao:
                        XiaoGuoQiPai();
                        ChiTao(player);
                        if(!m_arrayPlayer[player].isMe())
                            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
                        break;
                    case _Pai::Value_Chai:
                        XiaoGuoQiPai();
                        Chai(player,m_arrayStep[1].nSelectPlayerID);
                        break;
                    case _Pai::Value_Shun:
                        XiaoGuoQiPai();
                        Shun(player,m_arrayStep[1].nSelectPlayerID);
                        break;
                    case _Pai::Value_WuZhong:
                        XiaoGuoQiPai();
                        WuZhong(player);
                        break;
                    case _Pai::Value_JieDao:
                        XiaoGuoQiPai();
                        JieDao(player,m_arrayStep[1].nSelectPlayerID,m_arrayStep[2].nSelectPlayerID);
                        break;
                    case _Pai::Value_WanJian:
                        XiaoGuoQiPai();
                        WanJian(player);
                        break;
                    case _Pai::Value_NanMan:
                        XiaoGuoQiPai();
                        NanMan(player);
                        break;
                    case _Pai::Value_GaoKao:
                        XiaoGuoQiPai();
                        GaoKao(player,m_arrayStep[1].nSelectPlayerID);
                        break;
                    case _Pai::Value_ShanDian:
                        ShanDian(player, m_arrayPlayer[player].qiShouPai(m_arrayStep[0].nSelectValue[0],true));
                        if(!m_arrayPlayer[player].isMe())
                            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
                        break;
                    case _Pai::Value_LeBuSiShu:
                        Le(player, m_arrayStep[1].nSelectPlayerID, m_arrayPlayer[player].qiShouPai(m_arrayStep[0].nSelectValue[0],true));
                        if(!m_arrayPlayer[player].isMe())
                            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
                        break;
                    case _Pai::Value_LianNu:                //选的是武器
                    case _Pai::Value_ZhangBa:
                    case _Pai::Value_QingLong:
                    case _Pai::Value_GuanShiFu:
                    case _Pai::Value_HanBing:
                    case _Pai::Value_TanSu:
                        ZhuangBeiWuQi(player);
                        if(!m_arrayPlayer[player].isMe())
                            ShowChuPaiMessage(m_strMsg, m_nGameMessageDelay);
                        break;
                    default:
                        emit s_Critical(_T("[!GameCore]ChuPaiChuLi ERROR!"));
                        return setGameStatus(Game_Status_ERROR);
                    }
                }
                break;

            case _BaseObject::Area_ZhuangBei:
                switch(m_arrayStep[0].nSelectValue[0])
                {
                case _BaseObject::Area_WuQi:
                    switch(m_arrayPlayer[player].getZhuangBeiData((_BaseObject::_Area)(m_arrayStep[0].nSelectValue[0]))->value())
                    {
                    case _Pai::Value_ZhangBa:
                    case _Pai::Value_TanSu:
                        XiaoGuoQiPai();
                        Sha(player, m_arrayStep[2].nSelectPlayerID);
                        break;
                    default:
                        emit s_Critical(_T("[!GameCore]ChuPaiChuLi ERROR 2!"));
                        return setGameStatus(Game_Status_ERROR);
                    }
                    break;
                }
                break;

            case _BaseObject::Area_JiNeng:
                switch(m_arrayPlayer[player].getJiNengPaiData(m_arrayStep[0].nSelectValue[0])->value())
                {
                case _Pai::Value_JiNeng_ZhanZhengZhiShen:
                    //QiPai(m_arrayPlayer[player].qiShouPai(m_arrayStep[1].nSelectValue[0], true));
                    XiaoGuoQiPai();

                    m_strMsg = _T("%1 技能触发")
                            .arg(_JiNeng::getName(_JiNeng::JiNengValue_ZhanZhengZhiShen));
                    if(WenTi(player, m_strMsg, 0))
                    {
                        if(gameStatus() != Game_Status_Continue)return gameStatus();

                        //emit s_Message(_T("回答正确!"));
                        //ShowChuPaiMessage(str);
                        Sha(player, m_arrayStep[2].nSelectPlayerID,false);
                    }
                    break;

                case _Pai::Value_JiNeng_KaiSa:
                    //QiPai(m_arrayPlayer[player].qiShouPai(m_arrayStep[1].nSelectValue[0], true));
                    XiaoGuoQiPai();

                    //数量减1
                    m_arrayPlayer[player].getJiNengPaiData(m_arrayStep[0].nSelectValue[0])->m_listJiNeng.at(0)->m_nJiNengParam--;

                    if(GaoKao(player,m_arrayStep[2].nSelectPlayerID))    //如果高考成功
                    {
                        if(gameStatus() != Game_Status_Continue)return gameStatus();

                        ret = ChuPai(player,GameCore::Game_ChuPai_Type_Pai,_Pai::Value_JiNeng_KaiSa,-1,-1,0,3);
                        switch(ret)
                        {
                        case Game_Button_OK:
                            QiPai(m_arrayPlayer[m_arrayStep[2].nSelectPlayerID].qiShouPai(m_arrayStep[3].nSelectValue[0], true));
                            break;
                        case Game_Button_Cancel:
                            break;
                        default:
                            emit s_Critical(_T("[!GameCore]ChuPaiChuLi KaiSa ERROR!"));
                            return setGameStatus(Game_Status_ERROR);
                        }
                    }
                    break;

                case _Pai::Value_JiNeng_MengNaLiSha:
                    XiaoGuoQiPai();

                    m_strMsg = _T("%1 技能触发")
                            .arg(_JiNeng::getName(_JiNeng::JiNengValue_MengNaLiSha));
                    if(WenTi(player, m_strMsg, 0))
                    {
                        if(gameStatus() != Game_Status_Continue)return gameStatus();
                        Sha(player, m_arrayStep[2].nSelectPlayerID);
                    }
                    break;

                default:
                    emit s_Critical(_T("[!GameCore]ChuPaiChuLi JiNeng ERROR 3!"));
                    return setGameStatus(Game_Status_ERROR);
                }
                break;

            default:
                emit s_Critical(_T("[!GameCore]ChuPaiChuLi ERROR 3!"));
                return setGameStatus(Game_Status_ERROR);
            }

            if(gameStatus() != Game_Status_Continue)return gameStatus();
            emit s_RefreshAllPokePosition();
        }
        else if(ret == Game_Button_Cancel)
            continue;
        else if(ret == Game_Button_Giveup)
            break;
        else
        {
            emit s_Critical(_T("[!GameCore]ChuPaiChuLi ERROR 4!"));
            return setGameStatus(Game_Status_ERROR);
        }

    }

    return gameStatus();
}

GameCore::_GameStatus GameCore::QiPaiChuLi(int player)
{
    int n = 0;
    if(m_arrayPlayer[player].m_listShouPai.count() > m_arrayPlayer[player].m_nTiLi)    //手牌 大于上限
    {
        n = m_arrayPlayer[player].m_listShouPai.count() - m_arrayPlayer[player].m_nTiLi;
        //int i;

        if(n > 0)
        {
            if(ChuPai(player,GameCore::Game_ChuPai_Type_QiPai,0,n,n) == Game_Button_ERROR)
                return setGameStatus(Game_Status_ERROR);

            for(int i = 0; i < n; i++)
            {
                m_strMsg = _T("%1弃牌%2")
                        .arg(getPlayerShowName(player))
                        .arg(m_arrayPlayer[player].getShouPaiData(m_arrayStep[0].nSelectValue[i])->getName());
                emit s_Message(m_strMsg);
                QiPai(m_arrayPlayer[player].qiShouPai(m_arrayStep[0].nSelectValue[i],false));
            }
            m_arrayPlayer[player].tiaoZhengShouPaiDui();
        }

        /*
        for(; n > 0; n --)
        {
            for(;;)    //循环弃牌
            {
                if(MyTurn(player))    //玩家
                {
                    emit s_Message(_T("弃牌请输入手牌之前的编号，以回车结束！注：一次只能弃一张牌"));
                    cstr.Format(_T("手牌数：%d"),Player[player].shoupaishu);
                    emit s_Message(cstr);

                    //scanf("%d",&x);
                    x=WaitForInput();
                    if(x>=0 && x< Player[player].shoupaishu)
                        break;
                    else
                        emit s_Message(_T("你弃掉了一张空气！"));
                }
                else    //电脑随机弃牌
                {

                    break;
                }
            }
            cstr.Format(_T("%s弃牌%s"),Player[player].NameString(),Player[player].shoupai[x].PaiString());
            emit s_Message(cstr);

            QiPai(Player[player].shoupai[x]);
            Player[player].shoupai[x].leixing = -1;
            TiaoZhengPaiDui(player);

            if(MyTurn(player))
                Player[player].Kanshoupai();

            cstr.Format(_T("弃牌堆：%d"),qipaishu);
            emit s_Message(cstr);
        }
        */
    }
    m_arrayPlayer[player].resetAllFlags();
    emit s_RefreshAllPokePosition();
    emit s_RefreshAllFlags();
    //ShowChuPaiMessage("");
    //RefreshUI();

    return gameStatus();
}

GameCore::_GameStatus GameCore::HuiHeJieShuChuLi(int player)
{
    return HuiHeJieShuJiNeng(player);
}
